package com.gitee.shigen.sample.controller;

import java.time.Duration;

import javax.annotation.Resource;

import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import cn.hutool.core.date.DateUtil;

/**
 * @author shigenfu
 * @date 2023/12/10 18:38
 */
@RestController
@RequestMapping(value = "redis")
public class RedisTestController {

    @Resource
    private RedisTemplate<String, Object> redisTemplate;

    @GetMapping(value = "save")
    public Object save() {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("date", DateUtil.now());
        jsonObject.put("tag", "redis中文测试");

        JSONObject additionInfo = new JSONObject();
        additionInfo.put("name", "shigen");
        additionInfo.put("text", "shigen-cloud-platform的作者");
        jsonObject.put("additionInfo", additionInfo);
        redisTemplate.opsForValue().set("test", jsonObject, Duration.ofSeconds(120));
        return jsonObject;
    }

    @GetMapping(value = "get")
    public Object get() {
        return redisTemplate.opsForValue().get("test");
    }
}
